//
//  WBBaseViewController.swift
//  weibo
//
//  Created by swift on 17/2/22.
//  Copyright © 2017年 jason. All rights reserved.
import UIKit

class WBBaseViewController: UIViewController {
    //用户登录标记
    var vistorlognout:Bool = true
    //访客视图信息
    var visitorinfodict:[String:String]?
    ///表格视图 如果用户没有登录,就不创建
    var tableView:UITableView?
    //刷新控件
    var refreshControl: UIRefreshControl?
    //上拉刷新标记
    var isPullup:Bool = false
    
    ///自定义导航条(原来系统导航条被隐藏)
     lazy var navigationbar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 64))
     lazy var navitem = UINavigationItem()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        loaddata()
        
    }

    override var title: String?{
        didSet{
            navitem.title = title
        }
    }
    //准备加载数据 空的,具体由子类控制(覆盖)
    func loaddata(){
        refreshControl?.endRefreshing()
    }
}

extension WBBaseViewController{
      fileprivate func setupUI(){
        view.backgroundColor = UIColor.white
        
        //取消表格自动缩进
        automaticallyAdjustsScrollViewInsets = false
        
        //设置导航条
        setnavi()
        
        if vistorlognout{
            setuptableview()
        }else{
            setupvistorlogout()
        }
            }
    //设置访客视图
    private func setupvistorlogout(){
        let vistorview = WBvisitorView(frame: view.bounds)
        view.insertSubview(vistorview, belowSubview: navigationbar)
        vistorview.visitorinfo = visitorinfodict
        //登陆,注册按钮的监听
        vistorview.registerbutton.addTarget(self, action: #selector(regisiter), for: .touchUpInside)
        vistorview.logonbutton.addTarget(self, action: #selector(lognout), for: .touchUpInside)
        //设置导航按钮
        navitem.leftBarButtonItem = UIBarButtonItem(title: "注册", target: self, action: #selector(regisiter))
        navitem.rightBarButtonItem = UIBarButtonItem(title: "登陆", target: self, action: #selector(lognout))
    }
    //设置表格视图,暴露方便子类继承
     func setuptableview(){
        tableView = UITableView(frame: view.bounds, style: .plain)
        view.insertSubview(tableView!, belowSubview: navigationbar)
        tableView?.dataSource = self
        tableView?.delegate = self
        //设置内容缩进
        tableView?.contentInset = UIEdgeInsets(top: navigationbar.bounds.height, left: 0, bottom: tabBarController?.tabBar.bounds.height ?? 49, right: 0)
        
        ///设置刷新控件,下拉刷新
        refreshControl = UIRefreshControl()
        tableView?.addSubview(refreshControl!)
        //添加监听方法UIControlEventValueChanged
        refreshControl?.addTarget(self, action: #selector(loaddata), for: .valueChanged)
    }
    
    //设置导航条
    private func setnavi(){
        //添加导航条
        view.addSubview(navigationbar)
        //将item给bar
        navigationbar.items = [navitem]
        //设置bar的颜色
        navigationbar.barTintColor = UIColor.cz_color(withHex: 0xF6F6F6)
        //设置navbar title的字体颜色
        navigationbar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.darkGray]
        //设置
        navigationbar.tintColor = UIColor.orange
    }
}
//登陆,注册按钮的监听方法
extension WBBaseViewController{
    @objc fileprivate func regisiter(){
        print("注册")
    }
    @objc fileprivate func lognout(){
        print("登陆")
    }
    
}

extension WBBaseViewController:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return  0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    ///在显示最后一行,做上拉刷新
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        //1.判断indexpatch是否是最后一行
        //1row
        let row = indexPath.row
        //2section的个数
        let section = tableView.numberOfSections - 1
        
        
        if row<0 || section<0{
            return
        }
        //3最后一组数据行数
        let count = tableView.numberOfRows(inSection: section)
        //如果是最后一行,且同时没有上拉刷新
        if row == (count - 1) && !isPullup{
            //开始刷新
            print("上拉刷新")
            isPullup = true
            loaddata()
        }
        
    }
}
